package main;

import main.InputHelper;

/**
 * Created by arianne on 2014-07-12.
 */
public class PrimeFactor {
    public static void main(String[] args) {
        InputHelper ih = new InputHelper();
        Integer n = null;
        while (n == null) {
            n = ih.getUserInput("Prime factors for: ");
        }
        printPrimeFactors(n, 2);
    }

    static void printPrimeFactors(int n, int largestFactor) {
        if (n<2) return;
        else if (largestFactor == 2) {
            divBy2(n);
        } else {
            printNon2PrimeFactors(n, largestFactor);
        }
    }

    static void printNon2PrimeFactors(int n, int curFactor) {
        if (n%curFactor == 0) {
            System.out.print(curFactor);
            if (n != curFactor) System.out.print(",");
            printPrimeFactors(n/curFactor, curFactor);
        } else {
            printPrimeFactors(n, curFactor+=2);
        }
    }

    static void divBy2(int n) {
        if (n%2 == 0) {
            System.out.print("2");
            if (n != 2) System.out.print(",");
            printPrimeFactors(n/2,2);
        } else {
            printPrimeFactors(n,3);
        }
    }
}
